<template>
    <!--班级管理-->
    <div>
        <ToolBar>
            <el-button type="primary" icon="el-icon-plus" size="small" @click="visible=true">添加班级</el-button>
            <div class="searchBar">
                <el-input placeholder="请输入内容" v-model="fieldVal" size="small" style="width: 340px" clearable>
                    <el-select v-model="field" slot="prepend" placeholder="请选择" style="width:100px">
                        <el-option label="班级" value="title"></el-option>
                    </el-select>
                    <el-button @click="search" slot="append" icon="el-icon-search"></el-button>
                </el-input>
            </div>
        </ToolBar>
        <el-table v-loading="loading" :data='classes' border style="width: 100%">
            <el-table-column align="center" label="班级" width="250">
                <div slot-scope="{row}">
                    {{row.class_name}}班
                </div>
            </el-table-column>
            <el-table-column :formatter='formatCtime' align="center" label="创建时间" width="250">
            </el-table-column>
            <el-table-column :formatter='formatCtime2' align="center" label="更新时间" width="250">
            </el-table-column>
            <el-table-column label="操作" align="center" :render-header="tableAction">
                <template slot-scope="{row}">
                    <el-popover placement="top-start" width="50" trigger="hover" content="编辑" class="btnPop">
                        <el-button slot="reference" type="primary" icon="el-icon-edit" size='mini' @click='edit(row)' style="backgroundColor:rgb(238, 250, 255);color:#3C8DBC"></el-button>
                    </el-popover>
                    <el-popover placement="top-start" width="50" trigger="hover" content="删除" class="btnPop">
                        <el-button slot="reference" type="danger" icon="el-icon-delete" size='mini' @click='del(row.id)' style="backgroundColor:rgb(255,255,255);color:#f56c6c"></el-button>
                    </el-popover>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background layout="prev, pager, next ,sizes" :total="total" :page-sizes='[5,10,20]' :page-size='pageSize' :current-page.sync='currentPage' @current-change='currentChange' @size-change='sizeChange'></el-pagination>

        <!--添加班级-->
        <el-dialog :visible.sync='visible'>
            <div slot="title">
                <span style="color: #3c8dbc">- </span>添加班级
                <span style="color: #3c8dbc"> -</span>
            </div>
            <el-form status-icon label-position="right" ref="form" :model="form" :rules="rules">
                <el-form-item label="名称" label-width="70px" prop="rootName">
                    <el-input v-model="form.rootName" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="info" size="mini" @click="rootCancel('form')">取消</el-button>
                <el-button type="primary" size="mini" @click="rootSave('form')">添加</el-button>
            </div>
        </el-dialog>

        <!--编辑班级-->
        <el-dialog :visible.sync='editVisible'>
            <div slot="title">
                <span style="color: #3c8dbc">- </span>编辑班级
                <span style="color: #3c8dbc"> -</span>
            </div>
            <el-form status-icon label-position="right" ref="form" :model="form" :rules="rules">
                <el-form-item label="名称" label-width="70px" prop="editName">
                    <el-input v-model="form.editName" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="info" size="mini" @click="editCancel('form')">取消</el-button>
                <el-button type="primary" size="mini" @click="editSave('form')">保存</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import ToolBar from '@/components/ToolBar.vue';
import HelpHint from '@/components/HelpHint.vue';
import moment from 'moment'
export default {
    data() {
        return {
            id: '',
            field: '',
            fieldVal: '',
            classes: [],
            pageSize: 5,
            currentPage: 1,
            total: 0,
            loading: false,
            visible: false,
            editVisible: false,
            form: {
                rootName: '',
                editName: '',
            },
            id: '',
            rules: {
                rootName: [
                    { required: true, message: '请输入标题', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                editName: [
                    { required: true, message: '请输入标题', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ]
            },
        }
    },
    created() {
        this.getClasses();
    },
    methods: {
        getClasses() {
            this.loading = true;
            this.axios.get('/classes', {
                params: {
                    pageSize: this.pageSize,
                    currentPage: this.currentPage,
                    field: this.field,
                    fieldVal: this.fieldVal,
                }
            }).then((ret) => {
                this.loading = false;
                const { data } = ret.data;
                this.total = data.total;
                this.classes = data.rows;
            }).catch(() => { })
        },
        currentChange() {
            this.getClasses();
        },
        sizeChange(size) {
            this.pageSize = size;
            this.getClasses();
        },
        search() {
            this.getClasses();
        },
        del(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(() => {
                this.axios.delete('/classes/del/' + id).then(() => {
                    this.getClasses();
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => { })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        formatCtime: function(row) {
            return moment(row.ctime).format('YYYY-MM-DD HH-mm-ss')
        },
        formatCtime2: function(row) {
            return moment(row.mtime).format('YYYY-MM-DD HH-mm-ss')
        },
        handleClick(row) {
            this.$alert(row, '标题名称', {
                confirmButtonText: '确定',
                callback: action => {
                }
            });
        },
        tableAction() {
            return this.$createElement('HelpHint', {
                props: {
                    content: '编辑班级 / 删除班级 '
                }
            }, '操作');
        },
        rootSave(form) {
            this.$refs[form].validate((valid) => {
                if (valid) {
                    this.axios.post('/classes/add', {
                        class_name: this.form.rootName,
                        status: 1
                    }).then((ret) => {
                        this.getClasses();
                        this.visible = false;
                        this.form.rootName = '';
                    }).catch(() => { })
                } else {
                    this.$message.error('错了哦，请正确填写表单内容');
                }
            });
        },
        rootCancel(form) {
            this.$refs[form].resetFields();
            this.visible = false;
        },
        editSave(form) {
            this.$refs[form].validate((valid) => {
                if (valid) {
                    this.axios.put('/classes/edit/' + this.id, {
                        class_name: this.form.editName,
                        status: 1
                    }).then((ret) => {
                        this.getClasses();
                        this.editVisible = false;
                        this.form.editName = '';
                    }).catch(() => { })
                } else {
                    this.$message.error('错了哦，请正确填写表单内容');
                }
            });
        },
        editCancel(form) {
            this.$refs[form].resetFields();
            this.editVisible = false;
        },
        edit(data) {
            this.editVisible = true;
            this.id = data.id;
            this.form.editName = data.class_name;
        },
    },
    components: {
        ToolBar, HelpHint
    }
}
</script>
<style lang="less">
.el-select {
    display: block;
}

.searchBar {
    display: flex;
    float: right;
}

.el-tree-node {
    .el-tree-node__content {
        height: 50px;
        line-height: 50px;
    }
}

.el-popover--plain {
    padding: 5px;
}

.el-popover {
    min-width: 0;
    text-align: center;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0);
}

.el-table .cell .btnPop {
    margin: 10px;
}
</style>
